<?php

namespace app\controller\news;

use app\common\business\news\SubscripBus;
use app\controller\ApiController;

class Subscription extends ApiController
{
    /**
     * 订阅
     * @param SubscripBus $subscripBus
     * @return \support\Response
     * @throws \Exception
     */
    public function setSubscrip(SubscripBus $subscripBus)
    {
        $email = request()->post('email', '');
        if (empty($email)) {
            return self::error(trans('Please input your email'));
        }

        $res = $subscripBus::subscrip($email);
        if (!$res) {
            return self::error(trans('Subscription failed'));
        }
        return self::success([], trans('Subscription successful'));
    }

    /**
     * 获取订阅列表
     * @param SubscripBus $subscripBus
     * @return \support\Response
     */
    public function getSubscrip(SubscripBus $subscripBus)
    {
        $page = request()->post('page', 1, 'intval');
        $num = parent::paramNum();
        $res = $subscripBus->getDataByWhere([],'id,email,updated_at', $page, $num, 'updated_at desc');
        return self::success($res);
    }
}
